package com.interview.rtc.aigc.controller;

import com.interview.rtc.aigc.common.ResponseResult;
import com.interview.rtc.aigc.dao.UserDao;
import com.interview.rtc.aigc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@RestController
public class BaseController {

    @Autowired
    protected UserService userService;

    /**
     * 从请求中获取用户ID
     *
     * @return 用户ID
     */
    protected Long getUserId(  ) {

        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            return (Long) ((ServletRequestAttributes) requestAttributes).getRequest().getAttribute("userId");
        }
        return null;
    }

    /**
     * 获取当前用户信息
     *
     * @return 用户信息
     */
    protected ResponseResult<UserDao> getUserInfo() {
        Long userId = getUserId();
        UserDao user = userService.getUserById(userId);
        return ResponseResult.success(user);
    }

    protected UserDao getUser() {
        Long userId = getUserId();
        UserDao user = userService.getUserById(userId);
        return user;
    }
}
